<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>02天气案例监视属性实现</title>
  <script src="../js/vue.js"></script>
</head>
<body>
  <div id="app">
    <h1>{{title}}</h1>
    <h3>今天天气很{{info}}</h3>
    <button @click="changeWeather">改变天气</button>
  </div>
  <script>
    const vm = new Vue({
      el: '#app',
      data() {
        return {
          title: '天气案例监视属性实现',
          isHot: true
        }
      },
      computed: {
        info() {
          return this.isHot ? '炎热' : '凉爽'
        }
      },
      methods: {
        changeWeather() {
          this.isHot = !this.isHot
        }
      },
      /* watch: {
        isHot: {
          immediate: true,
          handler(newVal, oldVal) {
            console.log(newVal, oldVal)
          }
        }
      } */
    })
    /* 
      监视watch
        1 当被监视的属性变化时，handler回调函数自动调用，进行相关操作
        2 被监视的属性必须存在，才能进行监视
        3 监视的两种写法
          在watch配置项内书写
          通过vm.$watch()书写
    */
    vm.$watch('isHot', {
      immediate: true,
      handler(newVal, oldVal) {
        console.log(newVal, oldVal)
      }
    })
  </script>
</body>
</html>